home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / XGENERAL.C < prev    next >
C/C++ Source or Header  |  1992-02-25  |  5KB  |  163 lines

  1. /*****************************************************************************
  2. * A replacer for the MSDOS functions.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Aug. 1991   *
  5. *****************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "irit_sm.h"
  11.  
  12. /*****************************************************************************
  13. *   Routine to move a block in memory. Unlike memcpy/bcopy, this routine     *
  14. * should support overlaying blocks. This stupid implemetation will copy it   *
  15. * twice - to a temporary block and back again. The temporary block size will *
  16. * be allocated by demand.                             *
  17. *****************************************************************************/
  18. void movmem(VoidPtr Src, VoidPtr Dest, int Len)
  19. {
  20.     VoidPtr p = malloc(Len);
  21.  
  22.     memcpy(p, Src, Len);
  23.     memcpy(Dest, p, Len);
  24.  
  25.     free(p);
  26. }
  27.  
  28. /*****************************************************************************
  29. *   Routine to concat a full path to a given name - used in non MSDOS        *
  30. * environment only, and in that case assumes path is in IRIT_PATH variable.  *
  31. *****************************************************************************/
  32. char *searchpath(char *Name)
  33. {
  34.     static char FullPath[LINE_LEN_LONG];
  35.     char *p;
  36.  
  37.     if (p = getenv("IRIT_PATH")) {
  38.     strcpy(FullPath, p);
  39.     strcat(FullPath, Name);
  40.     }
  41.     else {
  42.     static int Printed = FALSE;
  43.     
  44.     strcpy(FullPath, Name);
  45.  
  46.     if (!Printed)
  47.     {
  48.         fprintf(stderr,
  49.             "IRIT_PATH env. not set. Only current directory is being searched.\n");
  50.         Printed = TRUE;
  51.     }
  52.     }
  53.     return FullPath;
  54. }
  55.  
  56. #ifdef STRICMP
  57. /*****************************************************************************
  58. *   Routine to compare two strings, ignoring case, up to given length.       *
  59. *****************************************************************************/
  60. int strnicmp(char *s1, char *s2, int n)
  61. {
  62.     /* Use to be simply 'return strncasecmp(s1, s2, n);' but seems that some */
  63.     /* unix systems (sun4?) does have it so here it is explicitly.         */
  64.     /* Written by Reiner Wilhelms <reiner@shs.ohio-state.edu>.             */
  65.     int i;
  66.     char c1, c2;
  67.  
  68.     for (i = 0; i < n; i++) {
  69.         if (islower(s1[i]))
  70.         c1 = toupper(s1[i]);
  71.         else
  72.         c1 = s1[i];
  73.         if (islower(s2[i]))
  74.         c2 = toupper(s2[i]);
  75.         else
  76.         c2 = s2[i];
  77.  
  78.         if (c1 != c2) {
  79.         if (c1 > c2) return 1;
  80.             if (c1 < c2) return -1;
  81.     }
  82.     }
  83.     return 0;
  84. }
  85.  
  86. /*****************************************************************************
  87. *   Routine to compare two strings, ignoring their case.             *
  88. *****************************************************************************/
  89. int stricmp(char *s1, char *s2)
  90. {
  91.     int i;
  92.     char *u1, *u2;
  93.  
  94.     if (s1 == NULL)
  95.     return s2 != NULL;
  96.     else if (s2 == NULL)
  97.     return 1;
  98.  
  99.     u1 = strdup(s1);
  100.     u2 = strdup(s2);
  101.  
  102.     for (i = 0; i < (int) strlen(u1); i++)
  103.     if (islower(u1[i])) u1[i] = toupper(u1[i]);
  104.     for (i = 0; i < (int) strlen(u2); i++)
  105.     if (islower(u2[i])) u2[i] = toupper(u2[i]);
  106.  
  107.     i = strcmp(u1, u2);
  108.  
  109.     free(u1);
  110.     free(u2);
  111.  
  112.     return i;
  113. }
  114. #endif /* STRICMP */
  115.  
  116. #ifdef STRSTR
  117. /*****************************************************************************
  118. *   Routine to search for pattern (no regular expression) in s. Returns      *
  119. * address in s of first occurance of patern, NULL if non found.             *
  120. *****************************************************************************/
  121. char *strstr(char *s, char *Pattern)
  122. {
  123.     int Len = strlen(Pattern);
  124.     char *p = (char *) s;
  125.  
  126.     while (p = strchr(p, Pattern[0]))
  127.     if (strncmp(p, Pattern, Len) == 0)
  128.         return p;
  129.     else
  130.         p++;
  131.  
  132.     return NULL;
  133. }
  134. #endif /* STRSTR */
  135.  
  136. #ifdef STRDUP
  137. /*****************************************************************************
  138. *   Routine to compare to strings, ignoring their case.                 *
  139. *****************************************************************************/
  140. char *strdup(char *s)
  141. {
  142.     char *p = malloc(strlen(s) + 1);
  143.  
  144.     if (p == NULL) return NULL;
  145.  
  146.     strcpy(p, s);
  147.  
  148.     return p;
  149. }
  150. #endif /* STRDUP */
  151.  
  152. #ifdef GETCWD
  153. /*****************************************************************************
  154. *   Get current working directory - BSD4.3.                     *
  155. *****************************************************************************/
  156. char *getcwd(char *s, int Len)
  157. {
  158.     getwd(s);
  159.  
  160.     return s;
  161. }
  162. #endif /* GETCWD */
  163.